package com.example.lending.View.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lending.Model.Book;
import com.example.lending.Model.Borrow;
import com.example.lending.Model.Database;
import com.example.lending.R;
import com.example.lending.ViewModel.BookViewModel;
import com.example.lending.ViewModel.BorrowViewModel;

import java.util.ArrayList;
import java.util.List;

public class BorrowAdapter extends RecyclerView.Adapter<BorrowAdapter.MyViewHolder> {
    private List<Borrow> borrows;
    private Context context;
    private BookViewModel bookViewModel;
    private BorrowViewModel borrowViewModel;
    private List<Book> books;
    public BorrowAdapter(Context context,List<Borrow> borrows,BorrowViewModel borrowViewModel,BookViewModel bookViewModel,List<Book>books) {
        this.borrows=borrows;
        this.context=context;
        this.bookViewModel=bookViewModel;
        this.borrowViewModel=borrowViewModel;
        this.books=books;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_borrow,parent,false);
        return new MyViewHolder(view);
    }
    public void setAllBorrow(List<Borrow> borrow){
        this.borrows=borrow;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if(holder instanceof MyViewHolder){
            final Borrow borrow= borrows.get(position);
            ((MyViewHolder) holder).tvName.setText("书名："+borrow.getName());
            ((MyViewHolder) holder).tvDuration.setText("剩余还书时间："+borrow.getDuration()+"天");
            ((MyViewHolder) holder).tvTime.setText("借书时间："+borrow.getTime());
            holder.tvGet.setText("id:"+borrow.getId());
            //((MyViewHolder) holder).tvState.setText(":"+borrow.getState());
            ((MyViewHolder) holder).button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) { //收藏按钮没有写
                    LiveData<List<Book>>livebook=bookViewModel.getAllBooksLive();
                    for(int i=0;i<books.size();i++)
                    {
                        if(books.get(i).getName().equals(borrow.getName())){
                            books.get(i).setNumber(0);
                            bookViewModel.updataBooks(books.get(i));
                        }
                    }
                    Toast.makeText(context, "还书成功", Toast.LENGTH_SHORT).show();
                    Borrow borrowToDelet = borrows.get(position);
                    borrowViewModel.deleteBorrows(borrowToDelet);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        if(borrows==null)
            return 0;
        return borrows.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvName,tvDuration,tvState,tvTime,tvGet;
        Button button;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName=itemView.findViewById(R.id.tv_nameborrow);
            tvTime=itemView.findViewById(R.id.tv_timeborrow);
            tvDuration=itemView.findViewById(R.id.tv_majorborrow);
            tvState=itemView.findViewById(R.id.tv_classborrow);
            tvGet=itemView.findViewById(R.id.tv_classborrow);
            button=itemView.findViewById(R.id.btn_backborrow);
        }
    }
}
